<?php
if(!isset($dbselect)) //前面没设置 数据库 则指定使用 DB3
    $dbselect   = Db::G3();
///关于标收费信息的统计
$sfwhere      = "";
if($startdate!="" && $enddate!=""){
  $sfwhere    = " and qtime_h>='$startdate' and  qtime_h<='$enddate'";  
} 
    const vip               = "vip";                     //  vip扣费
    const returnvip         = "returnvip";               //  vip返还
$rows = $dbselect->GetRows("SELECT sum(-money) as 利息管理费, count(money)  as 管理费次数,
b.cid,SUBSTR( qtime_h, 1, 10 ) as qtime_h
FROM  `04a_5flow_uid` f  left join 05b_0base b on f.busid =b.bid
WHERE  `flowty` = 'lixiglf' $sfwhere
group by SUBSTR( qtime_h, 1, 10 )");
foreach($rows as &$row){
    $cid    = $row['cid'];
    $ctime  = strtotime($row['qtime_h']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $count  = $row['管理费次数'];
 
    ///对于标一些特殊的资金变动的特殊统计 
    CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,"ttot_lixiguanlifei",$row['利息管理费'],$Y,$M,$D); //逾期还款罚息利润
    CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",1,"ttot_lixiguanlifei",$row['利息管理费'],$Y,$M,$D);
}
$rows = $dbselect->GetRows("SELECT sum(if(flowty='vip' and fuid_isBorrower(uid),-money,0)) as 会员费, sum(if(flowty='vip' and fuid_isBorrower(uid),1,0))  as 会员次数,
sum(if(flowty='vip',-money,0)) as 会员费a, sum(if(flowty='vip',1,0))  as 会员次数a,
sum(if(flowty='koukuan',-money,0)) as 其他扣费, sum(if(flowty='koukuan',1,0))  as 扣费次数,
sum(if(flowty='zhongjiefh',-money,0)) as 中介返点, sum(if(flowty='zhongjiefh',1,0))  as 中介返点次数,
sum(if(flowty='cuifaxi',-money,0)) as 催收罚息, sum(if(flowty='cuifaxi',1,0))  as 催收罚息次数,
sum(if(flowty='returnvip',money,0)) as 会员返还费, sum(if(flowty='returnvip',1,0))  as 会员返还次数,
sum(if(flowty='buchang',money,0)) as 特殊补偿费, sum(if(flowty='buchang',1,0))  as 特殊补偿费次数,
sum(if(flowty='qingsuan',-money,0)) as 清算费用, sum(if(flowty='qingsuan',1,0))  as 清算次数,
sum(if(flowty='jiangli',money,0)) as 网站奖励, sum(if(flowty='jiangli',1,0))  as 网站奖励次数,
sum(if(flowty='ruochefei',-money,0)) as 拖车费, sum(if(flowty='ruochefei',1,0))  as 拖车费次数,
sum(if(flowty='zhjianmian',money,0)) as 账户减免, sum(if(flowty='zhjianmian',1,0)) as 减免次数,
sum(if(flowty='diyafeiback',money,0)) as 押金返回, sum(if(flowty='diyafeiback',1,0))as 押金返回次数,
sum(if(flowty='returnfxj',money,0)) as 风险金返回, sum(if(flowty='returnfxj',1,0))as 风险金返回次数,
sum(if(flowty='returntcf',money,0)) as 停车费返回, sum(if(flowty='returntcf',1,0))as 停车费返回次数,
sum(if(flowty='returnglf',money,0)) as 管理费返回, sum(if(flowty='returnglf',1,0))as 管理费返回次数,
sum(if(flowty='maichekuisun',money,0)) as 卖车亏损, sum(if(flowty='maichekuisun',1,0))as 卖车亏损次数,
sum(if(flowty='bukoutcf',-money,0)) as 补扣停车费, sum(if(flowty='bukoutcf',1,0))as 补扣停车次数,
sum(if(flowty='bukouglf',-money,0)) as 补扣管理费, sum(if(flowty='bukouglf',1,0))as 补扣管理次数,
sum(if(flowty='jiajitixian',-money,0)) as 加急提现费, sum(if(flowty='jiajitixian',1,0))as 加急提现次数,
sum(if(flowty='dianzhissback',-money,0)) as 垫资损失收回费, sum(if(flowty='dianzhissback',1,0))as 垫资损失收回次数,
sum(if(flowty='huodongjiangli',money,0)) as 活动奖励, sum(if(flowty='huodongjiangli',1,0))as 活动奖励次数,
sum(if(flowty='huazhangss',-money,0)) as 坏账损失, sum(if(flowty='huazhangss',1,0))as 坏账损失次数,
cid,SUBSTR( qtime_h, 1, 10 ) as qtime_h
FROM  `04a_5flow_uid` f  
WHERE  `flowty` in ('vip','koukuan','zhongjiefh','cuifaxi','returnvip','buchang','qingsuan','jiangli','ruochefei','zhjianmian','diyafeiback','returnfxj','returntcf','returnglf','bukoutcf','maichekuisun','bukouglf','jiajitixian','dianzhissback','huodongjiangli','huazhangss') $sfwhere
group by cid,SUBSTR( qtime_h, 1, 10 )");
foreach($rows as &$row){
    $cid    = $row['cid'];
    $ctime  = strtotime($row['qtime_h']);
    $Y      = date("Y", $ctime);
    $M      = date("n", $ctime);
    $D      = date("j", $ctime); 
    $count  = $row['会员次数'];
    $counta = $row['会员次数a'];
    $co_ot  = $row['扣费次数'];
    $co_bc  = $row['会员返还次数'];
    $co_ts  = $row['特殊补偿费次数'];
    $co_qs  = $row['清算次数'];
    $co_wj  = $row['网站奖励次数'];
    $co_tc  = $row['拖车费次数'];
    $co_jm  = $row['减免次数'];
    $c_b_u  = $row['押金返回次数'];
    $c_b_f  = $row['风险金返回次数'];
    $c_b_t  = $row['停车费返回次数'];
    $c_b_g  = $row['管理费返回次数'];
    $c_b_zj = $row['中介返点次数'];
    $c_b_cf = $row['催收罚息次数'];
    $c_b_mk = $row['卖车亏损次数'];
    $c_b_bk = $row['补扣停车次数'];
    $c_b_gl = $row['补扣管理次数'];
    $c_j_tx = $row['加急提现次数'];
    $c_d_ss = $row['垫资损失收回次数'];
    $c_h_jl = $row['活动奖励次数'];
    $c_h_ss = $row['坏账损失次数'];
    ///对于标一些特殊的资金变动的特殊统计 
    if($c_b_u>0){
        CTLog::G()->doctAndDayReal($c_b_u.",rp_{$Y}{$m}_st01",1,"tot_yajinfh",$row['押金返回'],$Y,$M,$D); //逾期还款罚息利润
        CTLog::G()->doctAndDayReal($c_b_u.",rp_{$Y}{$m}_cid",$cid,"tot_yajinfh",$row['押金返回'],$Y,$M,$D);
    }
    if($c_b_f>0){
        CTLog::G()->doctAndDayReal($c_b_f.",rp_{$Y}{$m}_st01",1,"tot_fxj_back",$row['风险金返回'],$Y,$M,$D); //逾期还款罚息利润
        CTLog::G()->doctAndDayReal($c_b_f.",rp_{$Y}{$m}_cid",$cid,"tot_fxj_back",$row['风险金返回'],$Y,$M,$D);
    }
    if($c_b_t>0){
        CTLog::G()->doctAndDayReal($c_b_t.",rp_{$Y}{$m}_st01",1,"tot_tcf_back",$row['停车费返回'],$Y,$M,$D); //逾期还款罚息利润
        CTLog::G()->doctAndDayReal($c_b_t.",rp_{$Y}{$m}_cid",$cid,"tot_tcf_back",$row['停车费返回'],$Y,$M,$D);
    }
    if($c_b_g>0){
        CTLog::G()->doctAndDayReal($c_b_g.",rp_{$Y}{$m}_st01",1,"tot_glf_back",$row['管理费返回'],$Y,$M,$D); //逾期还款罚息利润
        CTLog::G()->doctAndDayReal($c_b_g.",rp_{$Y}{$m}_cid",$cid,"tot_glf_back",$row['管理费返回'],$Y,$M,$D);
    }
    ///对于标一些特殊的资金变动的特殊统计 
    if($count>0){
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_st01",1,"tot_vip_money",$row['会员费'],$Y,$M,$D); 
        CTLog::G()->doctAndDayReal($count.",rp_{$Y}{$m}_cid",$cid,"tot_vip_money",$row['会员费'],$Y,$M,$D);
    }
    if($counta>0){
        CTLog::G()->doctAndDayReal($counta.",rp_{$Y}{$m}_st01",1,"tot_avip_money",$row['会员费a'],$Y,$M,$D); 
        CTLog::G()->doctAndDayReal($counta.",rp_{$Y}{$m}_cid",$cid,"tot_avip_money",$row['会员费a'],$Y,$M,$D);
    }
    if($co_ot>0){
        CTLog::G()->doctAndDayReal($co_ot.",rp_{$Y}{$m}_st01",1,"tot_qt_koukuan",$row['其他扣费'],$Y,$M,$D);
        CTLog::G()->doctAndDayReal($co_ot.",rp_{$Y}{$m}_cid",$cid,"tot_qt_koukuan",$row['其他扣费'],$Y,$M,$D);
    }
    if($co_bc>0){
        CTLog::G()->doctAndDayReal($co_bc.",rp_{$Y}{$m}_st01",1,"tot_vip_back",$row['会员返还费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($co_bc.",rp_{$Y}{$m}_cid",$cid,"tot_vip_back",$row['会员返还费'],$Y,$M,$D);
    }
    if($co_ts>0){
        CTLog::G()->doctAndDayReal($co_ts.",rp_{$Y}{$m}_st01",1,"tot_spc_pay_back",$row['特殊补偿费'],$Y,$M,$D); 
        CTLog::G()->doctAndDayReal($co_ts.",rp_{$Y}{$m}_cid",$cid,"tot_spc_pay_back",$row['特殊补偿费'],$Y,$M,$D);
    }
    if($co_qs>0){
        CTLog::G()->doctAndDayReal($co_qs.",rp_{$Y}{$m}_st01",1,"tot_user_clear",$row['清算费用'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($co_qs.",rp_{$Y}{$m}_cid",$cid,"tot_user_clear",$row['清算费用'],$Y,$M,$D);
    }
    if($co_wj>0){
        CTLog::G()->doctAndDayReal($co_wj.",rp_{$Y}{$m}_st01",1,"tot_site_jiangli",$row['网站奖励'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($co_wj.",rp_{$Y}{$m}_cid",$cid,"tot_site_jiangli",$row['网站奖励'],$Y,$M,$D);
    }
    if($co_tc>0){
        CTLog::G()->doctAndDayReal($co_tc.",rp_{$Y}{$m}_st01",1,"tot_touchefei",$row['拖车费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($co_tc.",rp_{$Y}{$m}_cid",$cid,"tot_touchefei",$row['拖车费'],$Y,$M,$D);
    }
    if($co_jm>0){
        CTLog::G()->doctAndDayReal($co_jm.",rp_{$Y}{$m}_st01",1,"tot_site_jianmian",$row['账户减免'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($co_jm.",rp_{$Y}{$m}_cid",$cid,"tot_site_jianmian",$row['账户减免'],$Y,$M,$D);
    }
    if($c_b_zj>0){
        CTLog::G()->doctAndDayReal($c_b_zj.",rp_{$Y}{$m}_st01",1,"tot_b_zjf_lr",$row['中介返点'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_b_zj.",rp_{$Y}{$m}_cid",$cid,"tot_b_zjf_lr",$row['中介返点'],$Y,$M,$D);
    }
    if($c_b_cf>0){
        CTLog::G()->doctAndDayReal($c_b_cf.",rp_{$Y}{$m}_st01",1,"tot_site_csfx",$row['催收罚息'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_b_cf.",rp_{$Y}{$m}_cid",$cid,"tot_site_csfx",$row['催收罚息'],$Y,$M,$D);
    }
    if($c_b_mk>0){
        CTLog::G()->doctAndDayReal($c_b_mk.",rp_{$Y}{$m}_st01",1,"tot_b_maiche_ks",$row['卖车亏损'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_b_mk.",rp_{$Y}{$m}_cid",$cid,"tot_b_maiche_ks",$row['卖车亏损'],$Y,$M,$D);
    }
    if($c_b_bk>0){
        CTLog::G()->doctAndDayReal($c_b_bk.",rp_{$Y}{$m}_st01",1,"tot_site_bktcf",$row['补扣停车费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_b_bk.",rp_{$Y}{$m}_cid",$cid,"tot_site_bktcf",$row['补扣停车费'],$Y,$M,$D);
    }
    if($c_b_gl>0){
        CTLog::G()->doctAndDayReal($c_b_gl.",rp_{$Y}{$m}_st01",1,"tot_site_bkglf",$row['补扣管理费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_b_gl.",rp_{$Y}{$m}_cid",$cid,"tot_site_bkglf",$row['补扣管理费'],$Y,$M,$D);
    }
    if($c_j_tx>0){
        CTLog::G()->doctAndDayReal($c_j_tx.",rp_{$Y}{$m}_st01",1,"tot_site_jjtxf",$row['加急提现费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_j_tx.",rp_{$Y}{$m}_cid",$cid,"tot_site_jjtxf",$row['加急提现费'],$Y,$M,$D);
    }
    if($c_d_ss>0){
        CTLog::G()->doctAndDayReal($c_d_ss.",rp_{$Y}{$m}_st01",1,"tot_site_dzsssh",$row['垫资损失收回费'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_d_ss.",rp_{$Y}{$m}_cid",$cid,"tot_site_dzsssh",$row['垫资损失收回费'],$Y,$M,$D);
    }
    if($c_h_jl>0){
        CTLog::G()->doctAndDayReal($c_h_jl.",rp_{$Y}{$m}_st01",1,"tot_site_hdjjl",$row['活动奖励'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_h_jl.",rp_{$Y}{$m}_cid",$cid,"tot_site_hdjjl",$row['活动奖励'],$Y,$M,$D);
    }
    if($c_h_ss>0){
        CTLog::G()->doctAndDayReal($c_h_ss.",rp_{$Y}{$m}_st01",1,"tot_site_hzss",$row['坏账损失'],$Y,$M,$D);  
        CTLog::G()->doctAndDayReal($c_h_ss.",rp_{$Y}{$m}_cid",$cid,"tot_site_hzss",$row['坏账损失'],$Y,$M,$D);
    }
}
